﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace KaixinRobot
{

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
    //分析给定的文本，解析字符串等相关操作
    public class TextParser
    {
        private string content = null;
        private string startSign = null;
        private string endSign = null;
        private int curPos = 0;

        public TextParser(string con)
        {
            this.content = con;
        }

        public TextParser(string con, string start, string end)
        {
            this.content = con;
            this.startSign = start;
            this.endSign = end;
            this.curPos = 0;
        }

        public string getStrBtw(string start, string end)
        {
            this.curPos = 0;
            this.startSign = start;
            this.endSign = end;

            return this.getNextStrBtw();
        }

        public string getNextStrBtw()
        {
            if (string.IsNullOrEmpty(this.content)) return null;

            //Get string end pos
            int strBeginPos = 0;
            if (string.IsNullOrEmpty(this.startSign))
            {
                strBeginPos = this.curPos;
            }
            else
            {
                strBeginPos = this.content.IndexOf(this.startSign, this.curPos, StringComparison.CurrentCultureIgnoreCase);
                if (-1 == strBeginPos) return null;
            }
            strBeginPos += this.startSign.Length;

            //Get string end pos
            int strEndPos = 0;
            if (string.IsNullOrEmpty(this.endSign))
            {
                strEndPos = this.content.Length;
            }
            else
            {
                strEndPos = this.content.IndexOf(this.endSign, strBeginPos, StringComparison.CurrentCultureIgnoreCase);
                if (-1 == strEndPos) return null;
            }
            this.curPos = strEndPos + this.endSign.Length;

            //Return trimed string
            return this.content.Substring(strBeginPos, strEndPos - strBeginPos).Trim();
        }

        //1天3分
        //15小时35分
        //18小时整
        //35分
        public int getTotalMinutes()
        {
            string day = this.getStrBtw("", "天");
            if (null == day) day = "0";

            string hour = this.getStrBtw("天", "小时");
            if (null == hour)
            {
                hour = this.getStrBtw("", "小时");
            }
            if (null == hour) hour = "0";


            if (null == hour) hour = "0";
            string min = this.getStrBtw("小时", "分");
            if (null == min)
            {
                min = this.getStrBtw("天", "分");
            }
            if (null == min)
            {
                min = this.getStrBtw("", "分");
            }
            if (null == min) min = "0";


            int ret = GetInteger(day) * 24 * 60 + GetInteger(hour) * 60 + GetInteger(min);
            return ret;
        }

        public bool contains(string str)
        {
            return this.content.IndexOf(str) > -1;
        }

        public static int GetInteger(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return -1;
            }
            int id;

            if (int.TryParse(str, out id))
            {
                return id;
            }
            return -1;
        }

        //Read from the begining of str and combin all number it read to a number
        public static int ReadFirstInteger(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return -1;
            }

            int id;
            string subNbrStr = "";
            bool isReadingNbr = false;

            foreach (char c in str)
            {
                if (int.TryParse("" + c, out id))
                {
                    //Is Number
                    isReadingNbr = true;
                    subNbrStr += c;
                }
                else
                {
                    //Is Not Number
                    //如果当前正在读取数字，遇到非数字字符，则停止。
                    if (isReadingNbr) break;
                }
            }

            return GetInteger(subNbrStr);
        }

        public static List<int> GetIntegerList(string str)
        {
            string del = ",";
            List<int> result = new List<int>();
            if (string.IsNullOrEmpty(str))
            {
                return result;
            }
            string[] parts = str.Split(del.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            foreach (string part in parts)
            {
                int id = GetInteger(part);
                if (id != -1)
                {
                    result.Add(id);
                }
            }
            return result;
        }

        public static string NoHtml(string result)
        {
            if (result == null)
            {
                return string.Empty;
            }
            StringBuilder builder = new StringBuilder();
            bool flag = false;
            foreach (char ch in result)
            {
                switch (ch)
                {
                    case '<':
                        flag = true;
                        break;

                    case '>':
                        flag = false;
                        break;

                    default:
                        if (!flag)
                        {
                            if (ch == '\n')
                            {
                                builder.Append(" ");
                            }
                            else if (((ch != ' ') && (ch != '\t')) && (ch != '\r'))
                            {
                                builder.Append(ch);
                            }
                        }
                        break;
                }
            }
            return builder.ToString().Trim().Replace("&yen;", "￥").Replace("&nbsp;", "");
        }

        public static string GetVerifyCode(string content)
        {
            string quota = "\"";
            string Pattern = @"\s*?g_verify\s*?=\s*?" + quota + @"(.*?)\s*?" + quota + ";";
            Match verifyVar = Regex.Match(content, Pattern, RegexOptions.Multiline);
            return verifyVar.Result("$1").ToString().Trim();
        }

        private static Random ran = null;
        public static string RandomString()
        {
            if (ran == null)
            {
                long tick = DateTime.Now.Ticks;
                ran = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
            }

            double r = ran.NextDouble();
            return r.ToString();
        }

        public static string GetHtmlContent(string content, string label)
        {
            string start = "<" + label + ">";
            string end = "</" + label + ">";
            return (new TextParser(content)).getStrBtw(start, end);
        }

    }

}
